<?php

/**
 * worry actions.
 *
 * @package    jiazhang
 * @subpackage worry
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class worryActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
        $dql = Doctrine::getTable('Worry')->createQuery()->orderBy('created_at desc');
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('Worry',30);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  public function executeChange_worry_display(sfWebRequest $request){
       $display = (int)$request->getParameter('display',1);
       $id = (int)$request->getParameter('id',0);
       $this->forward404Unless($worry = Doctrine::getTable('Worry')->findOneById($id));
       $worry->setDisplay($display)->save();
       $this->getUser()->setFlash('success', '更新成功！');
       $this->redirect('worry/index');
  }
  
  public function executeShow(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      $id = (int)$request->getParameter('id',0);
      $this->forward404Unless($this->worry = Doctrine::getTable('Worry')->findOneById($id));
  }
  
  public function executeEdit_tags(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      $this->worry_id = (int)$request->getParameter('id',0);
      $this->worry_tags = Doctrine::getTable('WorryAndTag')->getWorryTagToString($this->worry_id);
  }
  
  public function executeCreate_tags(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      $worry_id = (int)$request->getParameter('worry_id',0);
      $tags = trim($request->getParameter('tags',''));
      $tags = explode(',', $tags);
      //删除所有关联
      Doctrine::getTable('WorryAndTag')->delWorryTag($worry_id);
      
      foreach($tags as $tag){
          if(strlen($tag) == 0)              continue;
          $tag_id = Doctrine::getTable('WorryTag')->addTag($tag);
          $worry_and_tag = new WorryAndTag();
          $worry_and_tag->setWorryId($worry_id)->setWorryTagId($tag_id)->save();
      }
      
      $this->getUser()->setFlash('success', '数据保存成功！');
      $this->redirect('worry/edit_tags?id='.$worry_id);
  }
}
